/*
	Description: Converts 1987 SIC codes into the sic87dd system
	
	Author: Brendan Price (modified from David Dorn original)
	Date last modified: September 6, 2013
*/

* Program for easy recoding
capture program drop assign 
program define assign
	* Verify that there are at least two entries: first argument is "recipient" code, others are "sources"
	assert "`2'" != ""
	local target "`1'"

	while "`2'" != "" {
		replace sic87dd = `target' if sic87dd == `2'
		
		macro shift
	}
end

* Use SIC codes as starting point
gen sic87dd = sic87

/*
	Modify codes as needed for the AADHP "Employment Sag" paper.
	This part of the script was introduced by Brendan Price.
*/

* Combining "Farm labor contractors" with "Farm management services" (not distinguished in the NAICS data)
assign 0760 0761
assign 0760 0762

* Combining "Ferroalloy ores, except vanadium" with "Metal ores n.e.c."
assign 1099 1060

* Combining "Chewing gum" with "Candy and other confectionary products"
assign 2064 2067

* Combining "Knit outerwear mills" with "Weft knit fabric mills"
assign 2257 2253

* Combining "Women's and children's underwear" with "Bras, girdles, and allied garments"
assign 2341 2342

* Combining "Primary nonferrous metals" with "Secondary nonferrous metals"
assign 3339 3340
assign 3339 3341

* Combining "Political organizations" with "Civic and social organizations"
assign 8640 8650

* Reassigning industries that would otherwise be unlinked to the trade data
assign 2531 2530
assign 2541 2540
assign 2631 2630
assign 3299 3292

* Reassigning all remaining "3-digit" industries (i.e. those that end in 0) that appear in the data
assign 2111 2110
assign 2121 2120
assign 2131 2130
assign 2141 2140
assign 2211 2210
assign 2221 2220
assign 2231 2230
assign 2241 2240
assign 2273 2270
assign 2311 2310
assign 2353 2350
assign 2371 2370
assign 2411 2410
assign 2611 2610
assign 2621 2620
assign 2711 2710
assign 2721 2720
assign 2741 2740
assign 2761 2760
assign 2771 2770
assign 2851 2850
assign 2911 2910
assign 3011 3010
assign 3021 3020
assign 3111 3110
assign 3131 3130
assign 3151 3150
assign 3161 3160
assign 3199 3190
assign 3211 3210
assign 3231 3230
assign 3241 3240
assign 3281 3280
assign 3743 3740
assign 3751 3750
assign 3812 3810
assign 3851 3850
assign 3861 3860
assign 3873 3870
assign 3931 3930

/*
	The following lines are taken from Dorn's original script subfile_sic87dd.do.
	Dorn refers to this as:
	
		Step 1a: Aggregate SIC Codes that are missing due to HS-SIC crosswalk, using 1992 product concordance.

	I modify one line, changing code 2092 to 0900 instead of to 0912.
*/	

replace sic87dd=2011 if sic87dd==2013
replace sic87dd=2099 if sic87dd==2038
replace sic87dd=2051 if sic87dd==2052
replace sic87dd=2051 if sic87dd==2053
replace sic87dd=2062 if sic87dd==2061
replace sic87dd=2062 if sic87dd==2063
replace sic87dd=0900 if sic87dd==2092
replace sic87dd=2252 if sic87dd==2251
replace sic87dd=2341 if sic87dd==2254
replace sic87dd=2392 if sic87dd==2259

replace sic87dd=2211 if sic87dd==2261
replace sic87dd=2221 if sic87dd==2262
replace sic87dd=2824 if sic87dd==2282
replace sic87dd=2325 if sic87dd==2326
replace sic87dd=2331 if sic87dd==2361
replace sic87dd=2389 if sic87dd==2387
replace sic87dd=2395 if sic87dd==2397
replace sic87dd=2449 if sic87dd==2441
replace sic87dd=2599 if sic87dd==2511
replace sic87dd=2599 if sic87dd==2512

replace sic87dd=2599 if sic87dd==2519
replace sic87dd=2599 if sic87dd==2521
replace sic87dd=2599 if sic87dd==2531
replace sic87dd=2599 if sic87dd==2541
replace sic87dd=2621 if sic87dd==2631
replace sic87dd=2621 if sic87dd==2671
replace sic87dd=2752 if sic87dd==2754
replace sic87dd=2752 if sic87dd==2759
replace sic87dd=2874 if sic87dd==2875
replace sic87dd=3069 if sic87dd==3061

replace sic87dd=3089 if sic87dd==3086
replace sic87dd=3089 if sic87dd==3087
replace sic87dd=3312 if sic87dd==3316
replace sic87dd=3312 if sic87dd==3317
replace sic87dd=3321 if sic87dd==3322
replace sic87dd=3321 if sic87dd==3324
replace sic87dd=3321 if sic87dd==3325
replace sic87dd=3357 if sic87dd==3355
replace sic87dd=3365 if sic87dd==3363
replace sic87dd=3499 if sic87dd==3364

replace sic87dd=3499 if sic87dd==3366
replace sic87dd=3499 if sic87dd==3369
replace sic87dd=3499 if sic87dd==3451
replace sic87dd=3499 if sic87dd==3463
replace sic87dd=3482 if sic87dd==3483
replace sic87dd=3496 if sic87dd==3495
replace sic87dd=3494 if sic87dd==3498
replace sic87dd=3577 if sic87dd==3575
replace sic87dd=3714 if sic87dd==3592
replace sic87dd=3648 if sic87dd==3645

replace sic87dd=3648 if sic87dd==3646
replace sic87dd=3711 if sic87dd==3716
replace sic87dd=3728 if sic87dd==3769

/*
	The following lines are taken from Dorn's original script subfile_sic87dd.do.
	Dorn refers to this as:
	
		Step 1b: Aggregate SIC Codes that are missing due to HS-SIC crosswalk, using hand matching.
*/

* Aggregation of 4-digit codes within the same 3-digit industry:

* finishing plants, nec to finishing plants, manmade (which in turn merges into broadwoven fabric mills, manmade)
replace sic87dd=2221 if sic87dd==2269
* books printing to books publishing
replace sic87dd=2731 if sic87dd==2732
* bookbinding and related work to blankbooks and looseleaf binding
replace sic87dd=2782 if sic87dd==2789
* typesetting to platemaking services
replace sic87dd=2796 if sic87dd==2791
* metal heat treating to primary metal products, nec
replace sic87dd=3399 if sic87dd==3398
* dolls and stuffed toys to games, toys, and children's vehicles
replace sic87dd=3944 if sic87dd==3942
* burial caskets to mfg industries nec
replace sic87dd=3999 if sic87dd==3995

* Other aggregation:

* plating and polishing to fabricated metal products, nec
replace sic87dd=3499 if sic87dd==3471
* metal coating and allied services to metal products, nec
replace sic87dd=3499 if sic87dd==3479

